//
//  RadioListViewController.m
//  Lianxi2
//
//  Created by lanou on 16/7/4.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "RadioListViewController.h"

@interface RadioListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation RadioListViewController
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    UIImageView *headView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3)];
    
    [RequestManager fetchDataWithURL:KRadioList parameter:@{@"radioid":self.mode.radioid} metod:@"POST" compelet:^(NSData *data) {
        //请求数据，分析数据，建立模型，添加数组
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            RadioModel *model = [[RadioModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
        }
        
        [headView sd_setImageWithURL:[NSURL URLWithString:bigDic[@"data"][@"radioInfo"][@"coverimg"]]];
        

    } updateUI:^{
        
        [self.tableview reloadData];
    }];
    
    
    
    
    
    self.tableview.tableHeaderView = headView;
   
    [self.tableview registerNib:[UINib nibWithNibName:@"RadioListCell" bundle:nil] forCellReuseIdentifier:@"radiolistcell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioModel *model = self.dataSource[indexPath.row];
    RadioListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"radiolistcell" forIndexPath:indexPath];
    [cell setCellWithModel:model];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioInfoViewController *a = [[RadioInfoViewController alloc]init];
    a.dataSource = self.dataSource;
    [self.navigationController pushViewController:a animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
